<template>
   <div>
       <Reply :data="data"  />
       <h1>课程提交页面</h1>

       <van-field v-model="title" label="课程标题" /><br>

       <van-field v-model="desc" label='课程描述' rows='5' text />

       所属类别: 
            <select name="" id="" v-model="cid">
                <option :value="i.id" v-for='i in titlelist'>{{i.name}}</option>
            </select><br>

       <van-uploader :after-read="afterRead" v-show='cid' ></van-uploader>

        <video :src="videoSrc" controls="controls" width="200px" height="200px" autoplay='autoplay' v-show='cid'></video>

            <br>

       <van-field v-model="price" label='价格' /> 

       <!-- 上线时间:<input type="date" v-model="datatime"><br> -->

       状态:
            <van-radio-group v-model="state" direction="horizontal">
                <van-radio :name='1'>上架</van-radio>
                <van-radio :name='0'>下架</van-radio>
            </van-radio-group>

        <van-button @click="addcourse">添加</van-button>

        

   </div>
</template>

<script>

import Reply from './Reply.vue';
import bus from './bus.js';
export default {
   data () {
      return {
            course:[],
            title:'',
            video:'',
            desc:'',
            cid:0,
            data:[],
            id:'',
            videoSrc:'',
            titlelist:'',
            state:1,
            price:'',
            datatime:'',
            uid:localStorage.getItem('id'),
      };
   },

   methods: {
       //添加视频
       afterRead(file){
            console.log(file)
            console.log(this.id)
            const param = new FormData();
            param.append("file", file.file);
            //课程course_id
            param.append("cid", this.id);
            param.append('id',this.uid);
            param.append('type',1)
            const myaxios=this.axios.create({withCredentials:false})
            myaxios({
                method:'post',
                url:this.localhost+'upload/',
                data:param
            }).then(res=>{
                console.log(res)
                this.videoSrc = this.localhost+'static/upload/'+localStorage.getItem('id')+'/'+res.data.filename
            })
    },
       //添加数据事件,datatime:this.datatime,price:this.price,state:this.state
       addcourse:function(){
           this.axios.post(this.localhost+'post_course/',{title:this.title,desc:this.desc,cid:this.cid,price:this.price,state:this.state,datatime:this.datatime})
           .then(res=>{
               console.log(res);
               this.getcourse()
           })
       },
       //获取分类
       get_cateall:function(){
           this.axios.get(this.localhost+'cateall/')
           .then(res=>{
               console.log(res);
               this.titlelist = res.data
           })
       },
       //获取课程
    getcourse(){
    this.axios(this.localhost+'get_course/')
      .then(res=>{
        console.log(res)
        this.course=res.data
      })
    }
        
   },

   computed: {},

   mounted() {
       this.get_cateall()
       this.getcourse()
   },

   components: {

		'Reply':Reply,
   },
   	//构造方法
	created() {  

		//监听
		bus.$on('msg',target => {
			console.log(target);
			this.fid = target;
		});
    }
}
</script>
<style lang='css' scoped>

</style>
